﻿using MediatR;
using System.Threading.Tasks;
using EWC.HIS.Domain.Entities;
using EWC.HIS.Domain;
using System.Threading;
using System;
using EWC.HIS.Domain.Repositories;
using EWC.Core.DependencyInjection;
using EWC.Core.Util.CustomException;
using EWC.HIS.Application.Contracts.AppointmentRegistrationService.Commands;

namespace EWC.HIS.Application.AppointmentRegistrations.Handlers
{
    public class DeleteAppointmentRegistrationCommandHandler : IRequestHandler<DeleteAppointmentRegistrationCommand, bool>, IScopedDependency
    {
        private readonly IAppointmentRegistrationRepository _appointmentRegistrationRepository;

        public DeleteAppointmentRegistrationCommandHandler(IAppointmentRegistrationRepository appointmentRegistrationRepository)
        {
            _appointmentRegistrationRepository = appointmentRegistrationRepository;
        }

        public async Task<bool> Handle(DeleteAppointmentRegistrationCommand request, CancellationToken cancellationToken)
        {
            var appointmentRegistration = await _appointmentRegistrationRepository.GetAsync(request.Id);

            if (appointmentRegistration == null)
            {
                //throw new ArgumentException("预约信息不存在");
                throw new UserFriendlyException("预约信息不存在");
            }
            return await _appointmentRegistrationRepository.DeleteAsync(appointmentRegistration);
        }
    }
}
